package com.ssi.action.index;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.ssi.action.CommonAction;
import com.ssi.domain.goods.Goods;
import com.ssi.service.goods.GoodsService;
import com.ssi.service.goods.impl.GoodsServiceImpl;
import com.ssi.service.goodstype.GoodstypeService;
import com.ssi.service.shop.ShopService;

/**
 * 
 * @author lxf
 *
 */
public class IndexAction extends CommonAction {

	private static final long serialVersionUID = 1L;
	private GoodsService goodsService=new GoodsServiceImpl();
	private ShopService shopService;
	private GoodstypeService goodstypeService;
	public GoodsService getGoodsService() {
		return goodsService;
	}
	public void setGoodsService(GoodsService goodsService) {
		this.goodsService = goodsService;
	}
	public ShopService getShopService() {
		return shopService;
	}
	public void setShopService(ShopService shopService) {
		this.shopService = shopService;
	}
	public GoodstypeService getGoodstypeService() {
		return goodstypeService;
	}
	public void setGoodstypeService(GoodstypeService goodstypeService) {
		this.goodstypeService = goodstypeService;
	}
	public String execute() throws Exception {
		return null;
	}
	
	/**
	 * 查询
	 * @return
	 */
	public String queryActivityGoodsShopSpread(){
		//GoodsService goodsService=new GoodsServiceImpl();
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>:queryActivitySpread");
		System.out.println("你好！ 开源中国");
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("ids", new int[]{1});
		map.put("top", 4);
		List<Goods> activityList=goodsService.queryGoodsByGoodstypeId(map);
		map.put("ids",new int[]{2});
		map.put("top", 10);
		List<Goods> shopList=goodsService.queryGoodsByGoodstypeId(map);
		map.put("ids", new int[]{3});
		map.put("top", 12);
		List<Goods> goodsList=goodsService.queryGoodsByGoodstypeId(map);
		List<Goods> shopSelfList=shopService.queryShopTop10(10);

		JSONObject jsonObject=new JSONObject();
		JSONArray activityJson=JSONArray.fromObject(activityList);
		JSONArray shopJson=JSONArray.fromObject(shopList);
		JSONArray goodsJson=JSONArray.fromObject(goodsList);
		JSONArray shopSelfJson=JSONArray.fromObject(shopSelfList);
		jsonObject.put("activity", activityJson);
		jsonObject.put("shop", shopJson);
		jsonObject.put("goods", goodsJson);
		jsonObject.put("shopSelf", shopSelfJson);

		
		json=jsonObject.toString();
		System.out.println(json);
		return "json";
		
	}
	public String queryMoreGoods(){
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>:queryMoreGoods");
		String spreadType = getParameters("spreadType");
		String goodsType =getParameters("goodsType");
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("spreadType", spreadType);
		map.put("goodsType",goodsType);
		List<Goods> listMoreGoods=goodsService.queryMoreGoods(map);
		JSONArray jsonArray=JSONArray.fromObject(listMoreGoods);
		json=jsonArray.toString();
		System.out.println(json);
		return "json";
		
	}
	public String queryNearBrowser(){
		Cookie[] cookies = super.request.getCookies();
		String[] ids = null; 
		for (Cookie cookie : cookies) {
			if(cookie.getName().equals("goodsIds")){
				String value = cookie.getValue();	
				 ids = value.split(",");
				
			}
		}
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("ids", ids);
		map.put("top", 10);
		goodsService.queryGoodsByIds(map);
		return json;
		
		
		
	}
	public String queryTypeGoods(){
		Map<String, Object> map=new HashMap<String, Object>();
		int[] ids={3,4,5,6,7,8};
		map.put("ids", ids);
		//map.put("top", 4);
		JSONObject jsonObject=new JSONObject();
		JSONArray yifu=new JSONArray();
		JSONArray shuma=new JSONArray();
		JSONArray yiyao=new JSONArray();
		JSONArray tushu=new JSONArray();
		JSONArray baihuo=new JSONArray();
		JSONArray qita=new JSONArray();
		List<Goods> listTypeGoods=goodsService.queryGoodsByGoodstypeId(map);
		for (int i = 0; i < listTypeGoods.size(); i++) {
			Goods goods = listTypeGoods.get(i);
			Integer goodsTypeId = goods.getGoodsTypeId();
			System.out.println(goodsTypeId);
			switch (goodsTypeId) {
			case 3:
				yifu.add(JSONObject.fromObject(goods));
				break;
			case 4:
				shuma.add(JSONObject.fromObject(goods));
				break;
			case 5:
				yiyao.add(JSONObject.fromObject(goods));
				break;
			case 6:
				tushu.add(JSONObject.fromObject(goods));
				break;
			case 7:
				baihuo.add(JSONObject.fromObject(goods));
				break;
			default:
				qita.add(JSONObject.fromObject(goods));
				break;
			}
			
		}
		jsonObject.put("yifu", yifu);
		jsonObject.put("shuma", shuma);
		jsonObject.put("yiyao", yiyao);
		jsonObject.put("tushu", tushu);
		jsonObject.put("baihuo", baihuo);
		jsonObject.put("qita", qita);
		json=jsonObject.toString();
		return "json";
		
	}

	public String queryActivitySpread() {
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>:queryActivitySpread");
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("ids", "1");
		map.put("top", "4");
		List<Goods> listGoods=goodsService.queryGoodsByGoodstypeId(map);
		JSONArray jsonArray=JSONArray.fromObject(listGoods);
		json=jsonArray.toString();
		System.out.println(json);
		return "json";
	}
	
	
	public String queryGoodsSpread() {
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>:queryActivitySpread");
		String goodstypeIds="3";
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("ids", "1");
		map.put("top", "4");
		List<Goods> listGoods=goodsService.queryGoodsByGoodstypeId(map);
		JSONArray jsonArray=JSONArray.fromObject(listGoods);
		json=jsonArray.toString();
		System.out.println(json);
		return "json";
	}
	public String queryShopSpread(){
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>:queryActivitySpread");
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("ids", "1");
		map.put("top", "4");
		List<Goods> listGoods=goodsService.queryGoodsByGoodstypeId(map);
		JSONArray jsonArray=JSONArray.fromObject(listGoods);
		json=jsonArray.toString();
		System.out.println(json);
		return "json";
		
	}
	
	
	
	public String queryShopSpreadSelf() {
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>:queryShopSpreadSelf");
		List<Goods> listGoods=shopService.queryShopTop10(10);
		JSONArray jsonArray=JSONArray.fromObject(listGoods);
		json=jsonArray.toString();
		System.out.println(json);
		return "json";
	}
	
	
	String json;
	public String getJson() {
		return json;
	}
	public void setJson(String json) {
		this.json = json;
	}
	

}
